package com.csweimei.rentmanagementtreasure.pop;

import android.content.Context;
import android.graphics.Color;
import android.widget.EditText;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.csweimei.rentmanagementtreasure.R;
import com.csweimei.rentmanagementtreasure.utils.ValidateUtils;
import com.lxj.xpopup.core.AttachPopupView;
import com.lxj.xpopup.core.BottomPopupView;

import java.util.ArrayList;
import java.util.List;

import me.goldze.mvvmhabit.utils.ToastUtils;

public class PopMonthRent extends BottomPopupView {

    private Context mContext;
    private PopSelClick mClick;
    private List<String> mList = new ArrayList<>();

    RecyclerView recyclerView;
    private String sel;
    private TextView cancel,sure;
    private EditText edtMin,edtMax;

    private boolean isClick = false;



    public PopMonthRent(@NonNull Context context, List<String> list, PopSelClick click) {
        super(context);
        this.mClick = click;
        mList.clear();
        mList.addAll(list);
    }

    @Override
    protected int getImplLayoutId() {
        return R.layout.pop_monthly_rent;
    }

    @Override
    protected void onCreate() {
        edtMin = findViewById(R.id.edt_min);
        edtMax = findViewById(R.id.edt_max);
        cancel = findViewById(R.id.cancel);
        sure = findViewById(R.id.sure);


        cancel.setOnClickListener(view ->{
            dismiss();
        });
        sure.setOnClickListener(view ->{
            if (ValidateUtils.isNotEmptyString(sel)){
                if (mClick != null){
                    if (isClick){
                        mClick.sel(new PopBean(sel));
                    }else {
                        sel = edtMin.getText().toString()+"-"+edtMax.getText().toString()+"元";
                        mClick.sel(new PopBean(sel));
                    }
                }
                dismiss();
            }else {
                ToastUtils.showShort("请选择或输入");
            }

        });

        recyclerView = findViewById(R.id.recycler);
        AttachAdapter adapter = new AttachAdapter();
        recyclerView.setAdapter(adapter);
        adapter.addChildClickViewIds(R.id.tv);
        adapter.setOnItemChildClickListener((adapter1, view, position) -> {
            adapter.setSel(position);
            String s = mList.get(position);
            if (s.equals("不限")){
                edtMin.setText("不限");
                edtMax.setText("不限");
                sel = "不限-不限";
            }else {
                String[] strings =s.split("-");
                edtMin.setText(strings[0]);
                edtMax.setText(strings[1].replace("元",""));
                sel = s;
            }



        });
        adapter.setList(mList);

    }

    private class AttachAdapter extends BaseQuickAdapter<String, BaseViewHolder>{

        private int index = -1;

        public AttachAdapter() {
            super(R.layout.item_month_rent);
        }

        public void setSel(int flag){
            index = flag;
            notifyDataSetChanged();
        }

        @Override
        protected void convert(@NonNull BaseViewHolder baseViewHolder, String s) {
            baseViewHolder.setText(R.id.tv,s);

            if (getItemPosition(s)== index){
                baseViewHolder.setBackgroundResource(R.id.tv,R.drawable.rect_main_8);
                baseViewHolder.setTextColorRes(R.id.tv,R.color.white);
            }else {
                baseViewHolder.setBackgroundResource(R.id.tv,R.drawable.rect_f6_8);
                baseViewHolder.setTextColorRes(R.id.tv,R.color.text_9d);
            }
        }
    }


}
